package com.xunyin.modules.area.rest;

import com.xunyin.annotation.AnonymousAccess;
import com.xunyin.api.ApiResult;
import com.xunyin.modules.area.param.TAreaQueryParam;
import com.xunyin.modules.area.service.TAreaSpaceService;
import com.xunyin.modules.area.vo.TAreaSpaceQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 城市区域 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-09-06
 */

@RestController
@RequestMapping
@Api(value = "城市区域", tags = "基础配置:城市区域")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AreaController {

    private final TAreaSpaceService areaSpaceService;

    /**
     * 城市区域列表
     */
    @AnonymousAccess
    @GetMapping("/areas")
    @ApiOperation(value = "城市区域列表", notes = "城市区域列表")
    public ApiResult<List<TAreaSpaceQueryVo>> areaList(TAreaQueryParam areaQueryParam) {
        if (areaQueryParam == null) {
            areaQueryParam = new TAreaQueryParam();
            areaQueryParam.setAreaLevel(1);
        }
        return ApiResult.ok(areaSpaceService.getList(areaQueryParam));
    }

    /**
     * 首页-根据城市获取城市信息
     */
    @PostMapping("/getInfoCity")
    @ApiOperation(value = "根据城市获取城市信息", notes = "根据城市获取城市信息")
    public ApiResult<Object> getInfoCity(@Validated @RequestBody TAreaQueryParam param) {
        String city = param.getAreaName();
        return ApiResult.ok(areaSpaceService.getInfoCity(city));
    }

    /**
     * 获取省市列表
     */
    @GetMapping("/listAreaByCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaParent", value = "父节点ID", paramType = "query", dataType = "int")
    })
    @ApiOperation(value = "获取省市列表", notes = "获取省市列表")
    public ApiResult<List<TAreaSpaceQueryVo>> listAreaByCode(@RequestParam(value = "areaParent") Integer areaParent) {
        return ApiResult.ok(areaSpaceService.listAreaByCode(areaParent));
    }


}
